
import {getCstIdStatementsList, getStatementById, exportStatement } from '../../services/capital/approval';
import Constants from '../../utils/Constants';
import { message } from "antd";
const R = require('ramda');

export default {
    namespace: 'statement',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list:[],
        detail:[],
        forexs:[],
        dataSource: {},
        statement: {},
    },
    reducers: {

        //-----------------setList

        /**
         * 设置结算单列表
         * @param state
         * @param payload
         * @returns {{list, page: *}}
         */
        setStatementList(state, { payload }) {
            return { ...state, list: payload.list, page: payload.page}
        },

        //--------------------setEntity


        /**
         * 设置结算单
         * @param state
         * @param action
         * @returns {{statement: (string|state.statement|{}|*)}}
         */
        setStatement(state, action) {
            let payload = action.payload;
            return {...state, statement: payload.statement, detail: payload.detail};
        },

    },
    effects: {




        /**
         * 获取结算单信息列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getStatementsList({payload}, { call, put, select }) {
            let page = yield select(state => state.statement.page);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            const res = yield call(getCstIdStatementsList, {
                stateno: R.isNil(payload) || R.isNil(payload.stateno) ? "" : payload.stateno,
                status: R.isNil(payload) || R.isNil(payload.status) ? '' : payload.status,
                cstId: R.isNil(payload) || R.isNil(payload.cstId) ? "" : payload.cstId,
                beginTime: R.isNil(payload) || R.isNil(payload.beginTime) ? "" : payload.beginTime,
                endTime: R.isNil(payload) || R.isNil(payload.endTime) ? "" : payload.endTime,
                minamount: R.isNil(payload) || R.isNil(payload.minamount) ? "" : payload.minamount,
                maxamount: R.isNil(payload) || R.isNil(payload.maxamount) ? "" : payload.maxamount,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setStatementList',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page : R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 根据id查询结算单信息
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getStatementById({ payload }, { call, put, select }) {
            const res = yield call(getStatementById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                let statement = res.data.content.statement;
                let detail = res.data.content.detail;
                let forexs = res.data.content.forexs;
                yield put({
                    type: "setStatement",
                    payload: {
                        statement: statement,
                        detail: detail,
                        forexs: forexs,
                    }
                });
            }
        },

        /**
         * 导出申请书
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *exportStatement({ payload }, { call, put, select }) {
            const res = yield call(exportStatement, {id: payload.id});
        },





    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {

            });
        }
    }
};